﻿package mortal.game.view.common.guide.stepOperater
{
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;

    public class AutoGuideStepBase extends Object
    {
        protected var _mask:GSprite;
        protected var _arrow:GuideArrow;
        protected var _updatePlaceTiemr:Timer;
        protected var _data:AutoGuideData;
        protected var _lightMask:SWFPlayer;
        protected var _rectLightMask:ScaleBitmap;
        protected var _circle:SWFPlayer;
        protected var _transRects:Array;
        protected var _guideTimerId:int = -1;
        protected var _tips:GTextFiled;
        protected var _tipsParent:DisplayObjectContainer;
        protected var _parentLayer:DisplayObjectContainer;
        protected var _childFilters:Array;
        protected var _child:DisplayObject;
        protected var _listening:Boolean = false;
        protected var _maskRect:Rectangle;
        protected var _targetPoint:Point;
        protected var _working:Boolean = false;

        public function AutoGuideStepBase()
        {
            this._maskRect = new Rectangle();
            this._targetPoint = new Point();
            return;
        }// end function

        protected function setData(param1:AutoGuideData, param2:DisplayObjectContainer = null, param3:DisplayObject = null, param4:DisplayObjectContainer = null) : void
        {
            this._working = true;
            this._data = param1;
            this._tipsParent = param2;
            this._child = param3;
            this._parentLayer = param4;
            if (this._tipsParent == null)
            {
                this._tipsParent = LayerManager.highestLayer;
            }
            if (param1.transparentRects != null && param1.transparentRects.length > 0)
            {
                this.updateTransparentRects(param1.transparentRects);
            }
            if (param1.rollingTips != null && param1.rollingTips.length > 0)
            {
                this.updateTips(param1.rollingTips);
            }
            if (param1.filterName != null && FilterConst[param1.filterName] != null && param1.filterName.length > 0 && this._child != null)
            {
                this._childFilters = this._child.filters;
                this._child.filters = [FilterConst[param1.filterName]];
            }
            if (this._guideTimerId > 0)
            {
                clearTimeout(this._guideTimerId);
            }
            var _loc_5:int = 0;
            if (this._data != null)
            {
                _loc_5 = this._data.guideTime;
            }
            if (_loc_5 == 0)
            {
                _loc_5 = 5000;
            }
            this._listening = false;
            if (param1.subType == AutoGuideActivedType.useItemEnd && param1.subTypeValue != null)
            {
                this._listening = true;
                Dispatcher.addEventListener(EventName.PackClickedToUseItem, this.clickedUseItemHandler);
            }
            this._guideTimerId = setTimeout(this.guideTimerOut, _loc_5);
            Dispatcher.addEventListener(EventName.StageResize, this.stageResizeHandler);
            return;
        }// end function

        protected function stageResizeHandler(event:DataEvent) : void
        {
            if (this._mask == null)
            {
                return;
            }
            DisplayUtil.drawHollowMaskByRect(this._mask, this._maskRect);
            return;
        }// end function

        private function clickedUseItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 == null)
            {
                return;
            }
            if (String(this._data.subTypeValue).indexOf(_loc_2.itemInfo.code.toString()) >= 0)
            {
                this.stop(this._data);
            }
            return;
        }// end function

        protected function updateTips(param1:String) : void
        {
            var _loc_2:Array = null;
            _loc_2 = param1.split(";");
            var _loc_3:* = GlobalStyle.textFormatPutong;
            _loc_3.font = FontUtil.xingkaiName;
            if (_loc_2.length >= 4)
            {
                _loc_3.size = int(_loc_2[3]);
            }
            if (_loc_2.length >= 5)
            {
                _loc_3.color = uint(_loc_2[4]);
            }
            this._tips = UIFactory.gTextField("", int(_loc_2[1]), int(_loc_2[2]), 600, 60, this._tipsParent, _loc_3);
            this._tips.mouseEnabled = false;
            this._tips.x = int(_loc_2[1]) + this._targetPoint.x;
            this._tips.y = int(_loc_2[2]) + this._targetPoint.y;
            this._tips.htmlText = _loc_2[0];
            return;
        }// end function

        protected function updateTransparentRects(param1:String) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_7:String = null;
            var _loc_8:DisplayObject = null;
            var _loc_9:Point = null;
            var _loc_10:String = null;
            var _loc_11:Array = null;
            var _loc_12:GSprite = null;
            if (this._mask == null)
            {
                return;
            }
            this._transRects = [];
            var _loc_2:* = param1.split("{}");
            var _loc_3:* = param1.split("#");
            if (_loc_2.length == 2)
            {
                _loc_3 = String(_loc_2[0]).split("#");
                if (this._parentLayer != null)
                {
                    _loc_7 = _loc_2[1];
                    _loc_8 = DisplayUtil.getChildByPropety(this._parentLayer, "name", _loc_7);
                    _loc_9 = _loc_8.localToGlobal(new Point());
                    _loc_4 = _loc_9.x;
                    _loc_5 = _loc_9.y;
                }
            }
            else
            {
                _loc_4 = this._maskRect.x;
                _loc_5 = this._maskRect.y;
            }
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3.length)
            {
                
                _loc_10 = _loc_3[_loc_6];
                _loc_11 = _loc_10.split(";");
                _loc_12 = UICompomentPool.getUICompoment(GSprite);
                _loc_12.graphics.clear();
                _loc_12.graphics.beginFill(16777215, 1);
                _loc_12.graphics.drawRect(0, 0, parseInt(_loc_11[2]), parseInt(_loc_11[3]));
                _loc_12.graphics.endFill();
                _loc_12.x = int(_loc_11[0]) + _loc_4;
                _loc_12.y = int(_loc_11[1]) + _loc_5;
                _loc_12.blendMode = BlendMode.ERASE;
                this._mask.blendMode = BlendMode.LAYER;
                this._mask.addChild(_loc_12);
                this._mask.cacheAsBitmap = false;
                this._transRects.push(_loc_12);
                _loc_6++;
            }
            return;
        }// end function

        protected function guideTimerOut() : void
        {
            if (this._guideTimerId < 0)
            {
                return;
            }
            this._guideTimerId = -1;
            this.stopMe();
            if (this._data.isBreakAfterMaskOut)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideBreakGuide, this._data));
            }
            if (GuideController.shouldResumeToTaskAfterGuide() && this._data.filterName != "NoMainEndType")
            {
                GameController.guide.resumeToTask();
            }
            return;
        }// end function

        protected function showCircleEffect(param1:DisplayObjectContainer, param2:DisplayObject, param3:String = "guideCircleEffect.swf") : void
        {
            var _loc_4:* = DisplayUtil.getRelativePlace(param1, param2);
            this.circle.load(param3, ModelType.NormalSwf, null);
            this._circle.move(_loc_4.x + param2.width / 2, _loc_4.y + param2.height / 2);
            param1.addChild(this._circle);
            return;
        }// end function

        protected function showCircleEffectByRect(param1:DisplayObjectContainer, param2:Rectangle, param3:String = "guideCircleEffect.swf") : void
        {
            this.circle.load(param3, ModelType.NormalSwf, null);
            this._circle.move(param2.x + param2.width / 2, param2.y + param2.height / 2);
            param1.addChild(this._circle);
            return;
        }// end function

        protected function showRectBitmapLightMask(param1:DisplayObjectContainer, param2:Rectangle) : void
        {
            if (this._rectLightMask == null)
            {
                this._rectLightMask = EffectManager.getTaskLightMask(param2.width, param2.height);
            }
            this._rectLightMask.x = param2.x;
            this._rectLightMask.y = param2.y;
            this._rectLightMask.width = param2.width;
            this._rectLightMask.height = param2.height;
            param1.addChild(this._rectLightMask);
            return;
        }// end function

        protected function showLightMask(param1:DisplayObjectContainer, param2:DisplayObject, param3:int = 16, param4:int = 16, param5:String = "guide_Rect_32_32.swf") : void
        {
            var _loc_6:* = DisplayUtil.getRelativePlace(param1, param2);
            this._lightMask = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
            this._lightMask.load(param5, ModelType.NormalSwf, null);
            this._lightMask.move(_loc_6.x + param3 + 3, _loc_6.y + param4 + 3);
            param1.addChild(this._lightMask);
            return;
        }// end function

        protected function showLightMaskByRect(param1:DisplayObjectContainer, param2:Rectangle, param3:int = 16, param4:int = 16, param5:String = "guide_Rect_32_32.swf") : void
        {
            this._lightMask = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
            this._lightMask.load(param5, ModelType.NormalSwf, null);
            this._lightMask.move(param2.x + param3, param2.y + param4);
            param1.addChild(this._lightMask);
            return;
        }// end function

        protected function showMask(param1:DisplayObjectContainer, param2:DisplayObject) : void
        {
            Game.scene.setMouseEnabled(false);
            var _loc_3:* = new Point();
            var _loc_4:* = param1.localToGlobal(_loc_3);
            var _loc_5:* = param2.localToGlobal(_loc_3);
            this.mask.x = -_loc_4.x;
            this.mask.y = -_loc_4.y;
            this._targetPoint.x = _loc_4.x;
            this._targetPoint.y = _loc_4.y;
            this._maskRect.x = _loc_5.x;
            this._maskRect.y = _loc_5.y;
            this._maskRect.width = param2.width + 2;
            this._maskRect.height = param2.height + 2;
            this.stageResizeHandler(null);
            param1.addChild(this.mask);
            return;
        }// end function

        protected function showMaskByRect(param1:DisplayObjectContainer, param2:Rectangle) : void
        {
            Game.scene.setMouseEnabled(false);
            var _loc_3:* = param1.localToGlobal(new Point());
            this.mask.x = -_loc_3.x;
            this.mask.y = -_loc_3.y;
            this._targetPoint.x = param2.x;
            this._targetPoint.y = param2.y;
            this._maskRect.x = param2.x + _loc_3.x;
            this._maskRect.y = param2.y + _loc_3.y;
            this._maskRect.width = param2.width;
            this._maskRect.height = param2.height;
            this.stageResizeHandler(null);
            param1.addChild(this.mask);
            return;
        }// end function

        protected function showArrowByChild(param1:DisplayObjectContainer, param2:AutoGuideData, param3:DisplayObject) : void
        {
            this.arrow.direction = param2.arrowDirection;
            this.arrow.label = HTMLUtil.getHtmlByComp(param2.arrowTips);
            var _loc_4:* = DisplayUtil.getRelativePlace(param1, param3);
            this._targetPoint.x = _loc_4.x;
            this._targetPoint.y = _loc_4.y;
            switch(param2.arrowDirection)
            {
                case GuideArrow.Down:
                {
                    this.arrow.x = _loc_4.x + param3.width / 2;
                    this.arrow.y = _loc_4.y - 4;
                    break;
                }
                case GuideArrow.Left:
                {
                    this.arrow.x = _loc_4.x + param3.width + 4;
                    this.arrow.y = _loc_4.y + param3.height / 2;
                    break;
                }
                case GuideArrow.Right:
                {
                    this.arrow.x = _loc_4.x - 4;
                    this.arrow.y = _loc_4.y + param3.height / 2;
                    break;
                }
                case GuideArrow.Up:
                {
                    this.arrow.x = _loc_4.x + param3.width / 2;
                    this.arrow.y = _loc_4.y + param3.height + 4;
                    break;
                }
                default:
                {
                    break;
                }
            }
            param1.addChild(this.arrow);
            return;
        }// end function

        protected function showArrowByRect(param1:DisplayObjectContainer, param2:AutoGuideData, param3:Rectangle) : void
        {
            this.arrow.direction = param2.arrowDirection;
            this.arrow.label = param2.arrowTips;
            this._targetPoint.x = param3.x;
            this._targetPoint.y = param3.y;
            switch(param2.arrowDirection)
            {
                case GuideArrow.Down:
                {
                    this.arrow.x = param3.x + param3.width / 2;
                    this.arrow.y = param3.y - 9;
                    break;
                }
                case GuideArrow.Left:
                {
                    this.arrow.x = param3.x + param3.width + 4;
                    this.arrow.y = param3.y + param3.height / 2;
                    break;
                }
                case GuideArrow.Right:
                {
                    this.arrow.x = param3.x - 4;
                    this.arrow.y = param3.y + param3.height / 2;
                    break;
                }
                case GuideArrow.Up:
                {
                    this.arrow.x = param3.x + param3.width / 2;
                    this.arrow.y = param3.y + param3.height + 9;
                    break;
                }
                default:
                {
                    break;
                }
            }
            param1.addChild(this.arrow);
            return;
        }// end function

        protected function get mask() : Sprite
        {
            if (this._mask == null)
            {
                this._mask = UICompomentPool.getUICompoment(GSprite);
                this._mask.cacheAsBitmap = true;
                this._mask.mouseEnabled = true;
                this._mask.mouseChildren = true;
            }
            return this._mask;
        }// end function

        protected function get circle() : SWFPlayer
        {
            if (this._circle == null)
            {
                this._circle = UICompomentPool.getUICompoment(SWFPlayer);
                this._circle.timeRate = 4;
            }
            return this._circle;
        }// end function

        protected function get arrow() : GuideArrow
        {
            if (this._arrow == null)
            {
                this._arrow = UICompomentPool.getUICompoment(GuideArrow) as GuideArrow;
            }
            return this._arrow;
        }// end function

        public function stop(param1:AutoGuideData = null) : void
        {
            this.stopMe();
            return;
        }// end function

        public function stopMe() : void
        {
            var _loc_1:int = 0;
            var _loc_2:GSprite = null;
            this._working = false;
            if (this._listening)
            {
                Dispatcher.removeEventListener(EventName.PackClickedToUseItem, this.clickedUseItemHandler);
                this._listening = false;
            }
            Game.scene.setMouseEnabled(true);
            if (this._guideTimerId >= 0)
            {
                clearTimeout(this._guideTimerId);
                this._guideTimerId = -1;
            }
            if (this._lightMask != null)
            {
                this._lightMask.gotoAndStop(1);
                this._lightMask.dispose(true);
                this._lightMask = null;
            }
            if (this._circle != null)
            {
                this._circle.gotoAndStop(1);
                this._circle.move(0, 0);
                this._circle.dispose(true);
                this._circle = null;
            }
            if (this._arrow != null)
            {
                this._arrow.dispose(true);
                this._arrow = null;
            }
            if (this._transRects != null && this._transRects.length > 0)
            {
                _loc_1 = 0;
                while (_loc_1 < this._transRects.length)
                {
                    
                    _loc_2 = this._transRects[_loc_1];
                    _loc_2.blendMode = BlendMode.NORMAL;
                    _loc_2.graphics.clear();
                    _loc_2.dispose(true);
                    _loc_1++;
                }
                this._transRects = null;
            }
            if (this._mask != null)
            {
                this._mask.blendMode = BlendMode.NORMAL;
                this._mask.graphics.clear();
                this._mask.dispose(true);
                this._mask = null;
            }
            if (this._tips != null)
            {
                this._tips.dispose(true);
                this._tips = null;
            }
            this._tipsParent = null;
            if (this._child != null)
            {
                this._child.filters = this._childFilters;
            }
            this._childFilters = null;
            this._child = null;
            if (this._rectLightMask != null)
            {
                this._rectLightMask.dispose();
                this._rectLightMask = null;
            }
            return;
        }// end function

    }
}
